package com.tobby.demo.jndi;

import java.util.Hashtable;

import javax.jms.ConnectionFactory;
import javax.jms.Topic;
import javax.jms.TopicConnectionFactory;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jndi.JndiTemplate;

import com.tibco.tibjms.naming.TibjmsFederatedTopic;
import com.tibco.tibjms.naming.TibjmsFederatedTopicConnectionFactory;

public class JndiDemo {

	public static void main(String[] args) throws NamingException {
		JndiDemo jndiDemo = new JndiDemo();
		ConnectionFactory connectionFactory = jndiDemo.getObjectByInitialContext();
		System.out.println("Get by InitialContext: " + connectionFactory);
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		connectionFactory = jndiDemo.getObjectByJndiTemplate(applicationContext);
		System.out.println("Get by JndiTemplate: " + connectionFactory);
		connectionFactory = jndiDemo.getObjectByJndiObjectFactoryBean(applicationContext);
		System.out.println("Get by JndiObjectFactoryBean" + connectionFactory);
		connectionFactory = jndiDemo.getObjectBySingleFactoryBean(applicationContext);
		System.out.println("Get by SingleFactoryBean" + connectionFactory);
		Topic outboundDestination = jndiDemo.getOubBoundDestination(applicationContext);
		System.out.println(outboundDestination);
	}

	private ConnectionFactory getObjectByInitialContext() throws NamingException {
		// "dns:" + dnsServer + "/" +domain
		// 下面这段程序代码通过查询URL中的Schema信息来自动选择Context的工厂类
		// dns schema 选择的工场类是com.sun.jndi.dns.DnsContextFactory。
		// Context context = new InitialContext();
		// DataSource ds = (DataSource)context.lookup("java:comp/env/jcptDataSourceJNDI");
		// Context context = new InitialContext();
		// ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("tibjmsnaming://EtcLnx31u.nam.nsroot.net:8222/TopicConnectionFactory");
		Hashtable<String, String> env = new Hashtable<String, String>();
		env.put(Context.INITIAL_CONTEXT_FACTORY, "com.tibco.tibjms.naming.TibjmsInitialContextFactory");
		env.put(Context.PROVIDER_URL, "tibjmsnaming://etclnx31u.nam.nsroot.net:8222");
		env.put(Context.SECURITY_PRINCIPAL, "PCSUser");
		env.put(Context.SECURITY_CREDENTIALS, "PCSUser");
		Context context = new InitialContext(env);
		TibjmsFederatedTopicConnectionFactory connectionFactory = (TibjmsFederatedTopicConnectionFactory) context.lookup("TopicConnectionFactory");
		return connectionFactory;
	}

	private ConnectionFactory getObjectByJndiTemplate(ApplicationContext applicationContext) throws NamingException {
		JndiTemplate jndiTemplate = (JndiTemplate) applicationContext.getBean("jndiTemplate");
		TibjmsFederatedTopicConnectionFactory connectionFactory = (TibjmsFederatedTopicConnectionFactory)jndiTemplate.lookup("TopicConnectionFactory");
		return connectionFactory;
	}

	private ConnectionFactory getObjectByJndiObjectFactoryBean(ApplicationContext applicationContext) {
		ConnectionFactory connectionFactory = (TopicConnectionFactory)applicationContext.getBean("topicConnectionFactory");
		return connectionFactory;
	}

	private ConnectionFactory getObjectBySingleFactoryBean(ApplicationContext applicationContext) {
		ConnectionFactory connectionFactory = (TopicConnectionFactory)applicationContext.getBean("connectionFactory");
		return connectionFactory;
	}

	private Topic getOubBoundDestination(ApplicationContext applicationContext) {
		TibjmsFederatedTopic oubBoundDestination = (TibjmsFederatedTopic)applicationContext.getBean("outbound");
		return oubBoundDestination;
	}

}
